package top.lixunda.ecommerce.server.good.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.entity.db.SysStoreCategoryDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.StoreCategoryInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.StoreCategoryUpdateDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.SysStoreCategoryTreeVO;
import top.lixunda.ecommerce.server.good.api.entity.vo.SysStoreCategoryVO;
import top.lixunda.ecommerce.server.good.api.controller.ISysStoreCategoryController;
import top.lixunda.ecommerce.server.good.service.ISysStoreCategoryService;

import java.util.List;

/**
 * 店铺分类管理，此店铺分类为系统店铺分类参数，超级管理员权限配置
 *
 * @author Xumda
 * @version time:2020/1/11 11:12
 */
@Slf4j
@RestController
public class SysStoreCategoryController extends BaseController
        implements ISysStoreCategoryController {

    private final ISysStoreCategoryService sysStoreCategoryService;

    @Autowired
    public SysStoreCategoryController(ISysStoreCategoryService sysStoreCategoryService) {
        this.sysStoreCategoryService = sysStoreCategoryService;
    }

    /**
     * 添加一个店铺分类
     *
     * @param storeCategoryInsertDTO 新添加的店铺分类数据
     * @return 是否添加成功
     */
    @Override
    public MessageObject<Boolean> insertStoreCategory(StoreCategoryInsertDTO storeCategoryInsertDTO) {
        boolean success = sysStoreCategoryService.insertStoreCategory(storeCategoryInsertDTO);
        return success ? getSuccessMessageObject("添加店铺分类数据成功", true) :
                getErrorMessageObject("添加店铺分类数据失败", false);
    }

    /**
     * 更新一个店铺分类
     *
     * @param storeCategoryUpdateDTO 需要更新的店铺分类数据
     * @return 是否更新成功
     */
    @Override
    public MessageObject<Boolean> updateStoreCategory(StoreCategoryUpdateDTO storeCategoryUpdateDTO) {
        boolean success = sysStoreCategoryService.updateStoreCategory(storeCategoryUpdateDTO);
        return success ? getSuccessMessageObject("更新店铺分类数据成功", true) :
                getErrorMessageObject("更新店铺分类数据失败", false);
    }

    /**
     * 根据id删除店铺分类，只删除该节点，其子节点全部放到父节点中去
     *
     * @param id 店铺分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeStoreCategoryById(Integer id) {
        boolean success = sysStoreCategoryService.removeStoreCategoryById(id);
        return success ? getSuccessMessageObject("移除店铺分类数据成功", true) :
                getErrorMessageObject("移除店铺分类数据失败", false);
    }

    /**
     * 根据id删除店铺分类，只删除该节点，其子节点全部放到父节点中去
     *
     * @param ids 店铺分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeStoreCategoryByIds(List<Integer> ids) {
        boolean success = sysStoreCategoryService.removeStoreCategoryByIds(ids);
        return success ? getSuccessMessageObject("移除店铺分类数据成功", true) :
                getErrorMessageObject("移除店铺分类数据失败", false);
    }

    /**
     * 根据id删除店铺分类，删除包括子节点的所有节点
     *
     * @param id 店铺分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeStoreCategoryAllById(Integer id) {
        boolean success = sysStoreCategoryService.removeStoreCategoryAllById(id);
        return success ? getSuccessMessageObject("移除店铺分类数据成功", true) :
                getErrorMessageObject("移除店铺分类数据失败", false);
    }

    /**
     * 根据id删除店铺分类，删除包括子节点的所有节点
     *
     * @param ids 店铺分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeStoreCategoryAllByIds(List<Integer> ids) {
        boolean success = sysStoreCategoryService.removeStoreCategoryAllByIds(ids);
        return success ? getSuccessMessageObject("移除店铺分类数据成功", true) :
                getErrorMessageObject("移除店铺分类数据失败", false);
    }

    /**
     * 根据id获取分类数据
     *
     * @param id 分类id
     * @return 单条分类数据
     */
    @Override
    public MessageObject<SysStoreCategoryVO> queryById(Integer id) {
        SysStoreCategoryDO sysStoreCategoryDO = sysStoreCategoryService.queryById(id);
        SysStoreCategoryVO vo = SysStoreCategoryVO.fromStoreCategoryDO(sysStoreCategoryDO);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 查询整个分类树
     *
     * @return 分类树
     */
    @Override
    public MessageObject<SysStoreCategoryTreeVO> queryTree() {
        List<SysStoreCategoryDO> list = sysStoreCategoryService.queryAll();
        SysStoreCategoryTreeVO vo = SysStoreCategoryTreeVO.fromStoreCategoryDoList(list);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 根据父节点id获取子类节点列表
     *
     * @param id 父节点id
     * @return 子节点列表
     */
    @Override
    public MessageObject<List<SysStoreCategoryVO>> queryChildrenByParentId(Integer id) {
        List<SysStoreCategoryDO> list = sysStoreCategoryService.queryChildrenByParentId(id);
        List<SysStoreCategoryVO> vo = SysStoreCategoryVO.fromStoreCategoryDO(list);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 查询首层节点
     *
     * @return 首层节点
     */
    @Override
    public MessageObject<List<SysStoreCategoryVO>> queryRoots() {
        List<SysStoreCategoryDO> list = sysStoreCategoryService.queryRoots();
        List<SysStoreCategoryVO> vo = SysStoreCategoryVO.fromStoreCategoryDO(list);
        return getSuccessMessageObject("获取数据成功", vo);
    }
}
